<?php

namespace app\store\controller\data;

use app\common\model\AppointmentOrder;
use app\store\controller\Controller;
use app\store\model\appointment\Time;
use app\store\model\appointment\Type;
use app\store\model\GoodsSku;
use app\store\model\Order;
use app\store\model\OrderGoods;
use app\store\model\User as UserModel;
use app\store\model\user\Grade as GradeModel;
use app\store\model\Order as OrderModel;
use think\Db;

/**
 * 用户数据控制器
 * Class User
 * @package app\store\controller\data
 */
class User extends Controller
{
    /* @var \app\store\model\User $model */
    private $model;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new UserModel;
        $this->view->engine->layout(false);
    }

    /**
     * 用户列表
     * @return mixed
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @throws \think\exception\DbException
     */
    public function lists($nickName = '', $gender = null, $grade = null)
    {
        // 会员等级列表
        $gradeList = GradeModel::getUsableList();
        // 用户列表
        $list = $this->model->getList($nickName, $gender, $grade);
        return $this->fetch('list', compact('list', 'gradeList'));
    }

    /**
     * 用户列表
     * @return mixed
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @throws \think\exception\DbException
     */
    public function lists_user($nickName = '')
    {
        // 用户列表
        $list = $this->model->getListByName($nickName);
        return $this->fetch('list_user', compact('list'));
    }

    /**
     * 用户列表
     * @return mixed
     * @param string $nickName 昵称
     * @throws \think\exception\DbException
     */
    public function lists_service($nickName = '')
    {
        // 用户列表
        $list = $this->model->getListByService($nickName);
        return $this->fetch('service', compact('list'));
    }

    /**
     * 获取未预约的订单
     * @return \think\Paginator
     */
    public function get_order()
    {
        $user_id = input('user_id',0);
        $orderModel = new OrderModel;
        $list = $orderModel->getUnUseOrder($user_id);

        return $this->fetch('order', compact('list'));
    }

    /*
     * 获取订单类型
     */
    public function get_order_type()
    {

        $order_id = input('order_id',0);

        $orderModel = new OrderGoods();
        $list = $orderModel->getOrderType($order_id);

        if($list){
            $data = [
                'code'=>1,
                'data'=>$list
            ];
        }else{
            $data = [
                'code'=>0,
                'data'=>$list
            ];
        }
        return json($data);

    }

    /*
     * 根据类型获取时间列表
     */
    public function get_time_list()
    {
        $type_id = input('type_id',1);
        $date_id = input('date_id');
        $date = strtotime($date_id);

        $list = Time::useTimeList($type_id,$date);
        return json($list);
    }

    /*
     * 根据类型获取服务人员
     */
    public function get_type_user()
    {
        $type_id = input('type_id',1);

        $list = Db::name('dealer_user')->where('find_in_set(:type_ids,type_ids)',['type_ids'=>$type_id])->field('user_id,real_name')->select();

        return json($list);
    }

    /*
     * 根据订单号获取订单下的商品
     */

    public function get_order_goods()
    {

        $order_id = input('order_id');

        $orderModel = new Order();
        $list = $orderModel->getGoodsList($order_id);

        return json($list);
    }

    /*
     * 根据规格显示预约类型
     */
    public function get_goods_type()
    {

        $goods_sku_id = input('goods_id');

        if(!$goods_sku_id){
            return json(['code'=>0,'msg'=>'goods_id必传']);
        }
        $orderModel = new GoodsSku();
        $list = $orderModel->getGoodsType($goods_sku_id);

        return json($list);
    }

    /*
     * 获取当前可用的服务人员
     */
    public function get_server_man()
    {
        $type_id = input('type_id');
        $date = input('date_id');
        //$time_id = input('time_id');

        $where = [
            'is_delete' => 0,
        ];

        //$time_arr = explode(',',$time_id);

        $date_time = strtotime($date);
        $sub_arr =[];
        $ar = [];
       /* foreach ($time_arr as $val){
            $ids = AppointmentOrder::where("appointment_date", $date_time)
                ->where('find_in_set(:time_id,time_id)',['time_id'=>$val])
                ->where("status", 'in', [1, 2])
                ->where($where)
                ->column('service_id');

            $sub_arr = array_merge($sub_arr, $ids);
        }
        $sub_arr = array_unique($sub_arr);
        $ar = array_merge($ar, $sub_arr);*/
        file_put_contents('ypp0001.php', "<?php\r\nreturn " . var_export($type_id, true) . "?>", FILE_APPEND);
        $all_server_man = \app\common\model\server\User::allServer($type_id, $ar);

        return json($all_server_man);
    }

    /*
     * 检测当前时间是否正确
     */
    public function checkTime()
    {
        $date = input('date_id');

        $date_time = strtotime($date);
        if($date_time < time()){
            return false;
        }
        return true;
    }
}